#!/usr/bin/env python

import sys
import os
import os.path
import shutil
import feedparser
from pprint import pprint

sys.path.insert(0, 'secore')
import secore

def setup_actions(db):
    db.add_field_action('feedtitle', secore.FieldActions.STORE_CONTENT)
    db.add_field_action('feeddescription', secore.FieldActions.STORE_CONTENT)
    db.add_field_action('feedlink', secore.FieldActions.STORE_CONTENT)
    db.add_field_action('feedlink', secore.FieldActions.COLLAPSE)
    db.add_field_action('feedlink', secore.FieldActions.INDEX_EXACT)

    db.add_field_action('time', secore.FieldActions.STORE_CONTENT)
    db.add_field_action('time', secore.FieldActions.SORTABLE, type="float")
    db.add_field_action('title', secore.FieldActions.STORE_CONTENT)
    db.add_field_action('title', secore.FieldActions.INDEX_FREETEXT)
    db.add_field_action('summary', secore.FieldActions.STORE_CONTENT)
    db.add_field_action('summary', secore.FieldActions.INDEX_FREETEXT)
    db.add_field_action('id', secore.FieldActions.INDEX_EXACT)
    db.add_field_action('id', secore.FieldActions.STORE_CONTENT)
    db.add_field_action('status', secore.FieldActions.INDEX_EXACT)
    db.add_field_action('status', secore.FieldActions.STORE_CONTENT)

def get_bool(entry, field):
    """Get a boolean value from a field.

    """
    val = entry.get(field)
    if val == '0': return False
    if not val: return False
    return True

class Indexer:
    def __init__(self, dbpath):
        self.db = secore.IndexerConnection(dbpath)
        self.added = 0

    def flush(self):
        self.db.flush()

    def index_entry(self, feed, entry):
        doc = secore.UnprocessedDocument()
        doc.fields.append(secore.Field('feedtitle', feed.get('feedtitle', '')))
        doc.fields.append(secore.Field('feedlink', feed.get('feedsource', '')))
        doc.fields.append(secore.Field('feeddescription', feed.get('feeddescription', '')))
        doc.fields.append(secore.Field('time', entry.get('time', '')))
        doc.fields.append(secore.Field('title', entry.get('title', '')))
        doc.fields.append(secore.Field('summary', entry.get('summary', '')))
        doc.fields.append(secore.Field('id', entry.get('id', '')))

        if get_bool(entry, 'mark'):
            doc.fields.append(secore.Field('status', 'M'))
        if get_bool(entry, 'readstatus'):
            doc.fields.append(secore.Field('status', 'R'))
        if get_bool(entry, 'updatestatus'):
            doc.fields.append(secore.Field('status', 'U'))
        if get_bool(entry, 'newstatus'):
            doc.fields.append(secore.Field('status', 'N'))

        self.db.add(doc)
        self.added += 1

    def index_cache_file(self, path):
        setup_actions(self.db)
        sys.stdout.write("Indexing ")
        d = feedparser.parse(path)
        sys.stdout.write("'%s' ..." % d.feed.get('feedtitle', d.feed.get('feeddescription', '')))
        count = 0
        for entry in d.entries:
            self.index_entry(d.feed, entry)
            count += 1
        sys.stdout.write(" %d entries\n" % count)


    def index_dir(self, topdirpath):
        for dirpath, dirnames, filenames in os.walk(topdirpath):
            for filename in filenames:
                path = os.path.join(dirpath, filename)
                self.index_cache_file(path)
                self.flush()

dbpath = 'feeddb'
shutil.rmtree(dbpath)
indexer = Indexer(dbpath)
indexer.index_dir('/home/richard/.liferea_1.2/cache/feeds')
indexer.flush()
